package com.javabasic.aspect;

import com.javabasic.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.weaver.reflect.JoinPointMatchImpl;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * @author: 晓帆
 * @description:
 * @create: 2021-06-28 15:53
 **/
@org.aspectj.lang.annotation.Aspect
@Component
public class Aspect {

    @Pointcut("execution(* com..*(..))")//指明切入点
    public void servicePointcut(){

    }
    @Before("servicePointcut()")
    public void before(){
        System.out.println("before");

    }
@Around("servicePointcut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    Object target = proceedingJoinPoint.getTarget();
    Class<?> aClass = target.getClass();
    Field userMapper = aClass.getDeclaredField("userMapper");
    Object mapper = sqlSession.getMapper(userMapper.getType());
    userMapper.setAccessible(true);
    userMapper.set(target,mapper);
    return proceedingJoinPoint.proceed();
}
    @After("servicePointcut()")
    public void after(){
        System.out.println("after");

    }
    @AfterReturning(value = "servicePointcut()",returning = "result")
    public void afterReturnIng(Object result){

    }
    @AfterThrowing(value = "servicePointcut()",throwing = "exception")
    public void afterThrowing(Exception exception){

    }
}
